package config;

import condition.LinuxCondition;
import condition.WindowsCondition;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/***
 * 连接数据库的配置
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
@Configuration
public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    /***
     * linux系统注入的数据源
     * @param lDriver
     * @param lUrl
     * @param lUsername
     * @param lPassword
     * @return
     */
    @Bean("dataSource")
    @Conditional(LinuxCondition.class)
    public DataSource createLinuxDataSource(@Value("${linux.driver}") String lDriver,
                                            @Value("${linux.url}") String lUrl,
                                            @Value("${linux.username}") String lUsername,
                                            @Value("${linux.password}") String lPassword) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(lUrl, lUsername, lPassword);
        dataSource.setDriverClassName(lDriver);
        System.out.println(lUrl);
        return dataSource;
    }

    /**
     * window系统注入的数据源
     * @return
     */
    @Bean("dataSource")
    @Conditional(WindowsCondition.class)
    public DataSource createWindowsDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        System.out.println(url);
        return dataSource;
    }


    //!!!方法重载时不起作用，要特别注意

//    /***
//     * linux系统注入的数据源
//     * @param lDriver
//     * @param lUrl
//     * @param lUsername
//     * @param lPassword
//     * @return
//     */
//    @Bean("dataSource")
//    @Conditional(LinuxCondition.class)
//    public DataSource createDataSource(@Value("${linux.driver}") String lDriver,
//                                       @Value("${linux.url}") String lUrl,
//                                       @Value("${linux.username}") String lUsername,
//                                       @Value("${linux.password}") String lPassword) {
//        DriverManagerDataSource dataSource = new DriverManagerDataSource(lUrl, lUsername, lPassword);
//        dataSource.setDriverClassName(lDriver);
//        System.out.println(lUrl);
//        return dataSource;
//    }
//
//    /**
//     * window系统注入的数据源
//     * @return
//     */
//    @Bean("dataSource")
//    @Conditional(WindowsCondition.class)
//    public DataSource createDataSource() {
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        dataSource.setDriverClassName(driver);
//        dataSource.setUrl(url);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        System.out.println(url);
//        return dataSource;
//    }



}
